package com.example.demo2.controller;

import com.example.demo2.entity.Sample;
import com.example.demo2.entity.SampleOrders;
import com.example.demo2.entity.Standard;
import com.example.demo2.mapper.SampleMapper;
import com.example.demo2.mapper.SpoMapper;
import com.example.demo2.mapper.StandardMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
public class SampleController {
    @Autowired
    SpoMapper spoMapper;
    @Autowired
    SampleMapper sampleMapper;
    @Autowired
    StandardMapper standardMapper;

    @GetMapping("/findSpoByUID")
    public List<SampleOrders> findSpoByUID(HttpSession session) {
        Integer uID = (Integer) session.getAttribute("uID");
        List<SampleOrders> sampleOrderss = spoMapper.findSpoByUID(uID);
        for(SampleOrders sampleOrders : sampleOrderss) {
            Integer spId=sampleOrders.getSpId();
            Sample sample = sampleMapper.findSampleBySpId(spId);
            if(sample!=null) {
                Standard standard = standardMapper.findStandardByStaId(sample.getStaId());
                sampleOrders.setSpName(sample.getSpName());
                if(standard!=null) {
                    sampleOrders.setStaName(standard.getStaName());
                    sampleOrders.setStaGB(standard.getStaGB());
                }
            }
        }
        return sampleOrderss;
    }

    @PutMapping("/updateSpo")
    public int updateSpo(int spId,String spNeed,String spGg) {
        return spoMapper.updateSpo(spId,spNeed,spGg);
    }

    @DeleteMapping("/deleteSpo")
    public int deleteSpo(int spId) {
        return spoMapper.deleteSpo(spId);
    }
}
